$libfx0 = @"
    [StructLayout(LayoutKind.Sequential)]
    public struct marshalStruct
    {
      public int array_length;
      public IntPtr array;
    };
    [DllImport("libfx0.dll")]
        public static extern void MarshalStructC(
                                         ref marshalStruct pointerinStruct);
    [DllImport("libfx0.dll")]
        public static extern void FreeMarshalStructptr(
                                         ref marshalStruct pointerinStruct);
"@

Add-Type -MemberDefinition $libfx0 -Namespace fx0 -Name "st_fx"

Function PrintCsharpArray([string]$title, [double[]]$CsharpArray, [int]$ArrayLength)
{
    for($i=0;$i-lt$ArrayLength;$i++){ 
        if($i-eq0){
            Write-Host -NoNewLine $title
        }
        Write-Host -NoNewLine " "$CsharpArray[$i] 
    }
    Write-Host
}

$pointerinStruct = New-Object fx0.st_fx+marshalStruct;
$pointerinStruct.array_length = 5

[fx0.st_fx]::MarshalStructC([ref]$pointerinStruct)
$Array=New-Object double[] 5;
### 将数据从非托管内存指针复制到一维的托管双精度浮点数组
[System.Runtime.InteropServices.Marshal]::Copy($pointerinStruct.array, $Array,0, 5)
PrintCsharpArray "Copy from C-Memory to PWSH-Array: " $Array 5

$Array[0]=2.0
$Array[1]=2.1
$Array[2]=2.2
$Array[3]=2.3
$Array[4]=2.4
PrintCsharpArray "Set New values to PWSH-Array: " $Array 5

### 将数据从一维的托管双精度浮点数组复制到非托管内存指针
[System.Runtime.InteropServices.Marshal]::Copy($Array, 0, $pointerinStruct.array, 5)
Write-Host "PWSH-Array's new values has copied to C-Memory."

$Array[0]=0.0
$Array[1]=0.0
$Array[2]=0.0
$Array[3]=0.0
$Array[4]=0.0
PrintCsharpArray "Flush PWSH-Array: " $Array 5

[System.Runtime.InteropServices.Marshal]::Copy($pointerinStruct.array, $Array,0, 5)
PrintCsharpArray "Copy from C-Memory to PWSH-Array: " $Array 5

[fx0.st_fx]::FreeMarshalStructptr([ref]$pointerinStruct)